// pages/Open/Open.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unpaid: false,
    unpaid_info: []
  },

  go_pay(){
    //检测是否关锁成功
    wx.request({
      url: getApp().globalData.server + '/index.php/Home/QiuBao/Find_Device_lock',
      data:{
        device_id: getApp().globalData.device_id
      },
      method:"POST",
      header: {
        'content-type': "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      success:function(res){
        console.log(res)
        if(res.data.lock_state == 1){
          wx.showModal({
            title:'关门检测',
            content:'请确认您已经关好箱门',
            success(res){
              if(res.confirm){
                console.log('用户点击确定')
              }else if(res.cancel){
                console.log('用户点击取消')
              }
            }
          })
        }else if(res.data.lock_state == 0){
          // console.log(res.data.data)
          wx.showToast({
            title: '正在为您生成订单，稍等',
            icon: 'none',
            duration: 3000, //弹出提示框时长
            mask: true,
            success(data) {
              setTimeout(function () {
                //要延时执行的代码
                wx.reLaunch({
                  url: '/pages/group_buy/group_buy'
                })
              }, 3000) //延迟时间
            }
          })
        }
      },
      fail:function(res){
        // wx.showModal({
        //   title:'哎呀~~',
        //   content:'网络不在状态呢！',
        //   success:function(res){
        //     if (res.confirm) {
        //       console.log("用户点击确定")
        //     } else if (res.cancel) {
        //       console.log("用户点击取消")
        //     }
        //   }
        // })
      },
      complete:function(res){
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },

  pay_bill:function(e){
    //与服务器交互---支付接口
    wx.login({
      success: res => {
        // code 用于获取openID的条件之一
        // var code = res.code;
        var openId = wx.getStorageSync('openId')
        var crsNo = getApp().globalData.unpaid_info.bill_id     //订单号
        var react_pay = getApp().globalData.unpaid_info.goods_price * getApp().globalData.unpaid_info.usetime - getApp().globalData.unpaid_info.use_coupon * getApp().globalData.unpaid_info.coupon_price     //获取实际支付金额
        wx.request({
          url: getApp().globalData.server + '/index.php/Home/Pay/make_order',
          method: "POST",
          data: {
            openid: openId,
            data_total: react_pay,
            goods_type: getApp().globalData.unpaid_info.goods_type,
            crsNo: crsNo
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {  //后端返回的数据
            var data = res.data;
            console.log(data);
            console.log(data["timeStamp"]);
            wx.requestPayment({
              timeStamp: data['timeStamp'],
              nonceStr: data['nonceStr'],
              package: data['package'],
              signType: data['signType'],
              paySign: data['paySign'],
              success: function (res) {
                //支付成功后调用修改订单支付状态接口
                wx.request({
                  url: getApp().globalData.server + '/index.php/Home/QiuBao/modify_bill_state',
                  data:{
                    bill_id: getApp().globalData.unpaid_info.bill_id
                  },
                  header: {
                    'content-type': "application/x-www-form-urlencoded"
                  },
                  method: 'POST',
                  dataType: 'json',
                  success:function(res){
                    console.log(res)
                    if(res.data.error_code != 0){
                      wx.showModal({
                        title:'哎呀~',
                        content:'出错了呢！' + res.data.msg,
                        success(res){
                          if(res.confirm){
                            console.log('用户点击确定')
                          }else if(res.cancel){
                            console.log('用户点击取消')
                          }
                        }
                      })
                    }else if(res.data.error_code == 0){
                      // console.log(res.data.data)
                      console.log("订单支付成功！")
                    }
                  },
                  fail:function(res){
                    wx.showModal({
                      title:'哎呀~~',
                      content:'网络不在状态呢！',
                      success:function(res){
                        if (res.confirm) {
                          console.log("用户点击确定")
                        } else if (res.cancel) {
                          console.log("用户点击取消")
                        }
                      }
                    })
                  },
                  complete:function(res){
                    wx.hideLoading({
                      success: (res) => {},
                    })
                  }
                })
                wx.showModal({
                  title: '支付成功',
                  content: '',
                  showCancel: false,
                  success (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.reLaunch({
                        url: '/pages/group_buy/group_buy',
                      })
                    }
                  }
                })
              },
              fail: function (res) {
                console.log("小程序这边调起支付失败啦")
                console.log(res);
              }
            })
          }
        });
      }
    })
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      //查询全局中有无未支付的账单，如果有，弹出未支付的账单
      this.setData({
        unpaid: getApp().globalData.unpaid,
        unpaid_info: getApp().globalData.unpaid_info
      })
      console.log(this.data.unpaid_info)

      wx.showLoading({
        title: '加载中',
      })
      setTimeout(function(){
        wx.hideLoading({
        success: (res) => {},
        })
      },5000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh:function()
  {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    
    //模拟加载
  
    setTimeout(function()
    {
      // complete
      
      wx.hideNavigationBarLoading() //完成停止加载
      
      wx.stopPullDownRefresh() //停止下拉刷新
    
    },1500);
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})